package com.limbo.showapp.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by wangqi on 2018/9/4.
 */

public class DisplayUtils {
    public static int getScreenWidth(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.widthPixels;
    }

    public static int getScreenHeight(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.heightPixels;
    }

    /**
     * 获取设备的唯一标识，deviceId
     *
     * @param context
     * @return
     */
    public static String getDeviceId(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        @SuppressLint("MissingPermission") String deviceId = tm.getDeviceId();
        return deviceId;
    }

    /**
     * 在{@link Activity#setContentView}之后调用
     *
     * @param context       要实现的沉浸式状态栏的Activity
     * @param titleViewGroup 头部控件的ViewGroup,若为null,整个界面将和状态栏重叠
     */
    public static void initAfterSetContentView(Context context,
                                               View titleViewGroup) {
        if (context == null)
            return;
        if (titleViewGroup == null)
            return;
        // 设置头部控件ViewGroup的PaddingTop,防止界面与状态栏重叠
        int statusBarHeight = getStatusBarHeight(context);
        titleViewGroup.measure(0, 0);
        int measuredHeight = titleViewGroup.getMeasuredHeight();
        ViewGroup.LayoutParams layoutParams = titleViewGroup.getLayoutParams();
        layoutParams.height = measuredHeight + statusBarHeight;
        // LogUtil.e("layoutParams.height=" + layoutParams.height);
        titleViewGroup.setLayoutParams(layoutParams);

        titleViewGroup.setPadding(titleViewGroup.getPaddingLeft(),
                titleViewGroup.getPaddingTop() + statusBarHeight,
                titleViewGroup.getPaddingRight(),
                titleViewGroup.getPaddingBottom());
    }

    /**
     * 获取状态栏高度
     *
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier(
                "status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}
